﻿using System;
using System.Linq;
using Dpy.MathWinRT.Geometry.Geometry2D;
using Dpy.MathWinRT;

namespace BouncingBallGame.ViewModels.ShapeViewModels
{
    public class CircleViewModel : ShapeViewModelBase
    {
        #region Fields

        protected double radius;
        protected double x;
        protected double y;

        #endregion


        #region Properties

        public Vector2D Center
        {
            get
            {
                return new Vector2D(this.x, this.y);
            }
        }

        public Circle Circle
        {
            get
            {
                return new Circle(this.Center, this.radius);
            }
        }

        public double XCenter
        {
            get
            {
                return this.x;
            }
            set
            {
                if (this.x != value && value.IsRealNumber())
                {
                    this.x = value;
                    this.OnPropertyChanged("X");
                }
            }
        }

        public double YCenter
        {
            get
            {
                return this.y;
            }
            set
            {
                if (this.y != value && value.IsRealNumber())
                {
                    this.y = value;
                    this.OnPropertyChanged("Y");
                }
            }
        }

        public override double X
        {
            get
            {
                return this.GetFalseX();
            }
            set
            {
                if (this.GetFalseX() != value && value.IsRealNumber())
                {
                    this.x = value + this.radius;
                    this.OnPropertyChanged("X");
                }
            }
        }

        public override double Y
        {
            get
            {
                return this.GetFalseY();
            }
            set
            {
                if (this.GetFalseY() != value && value.IsRealNumber())
                {
                    this.y = value + this.radius;
                    this.OnPropertyChanged("Y");
                }
            }
        }

        public double Radius
        {
            get
            {
                return this.radius;
            }
            set
            {
                if (this.radius != value)
                {
                    this.radius = value;
                    this.OnPropertyChanged("Radius");
                    this.OnPropertyChanged("Diameter");
                    this.OnPropertyChanged("X");
                    this.OnPropertyChanged("Y");
                }
            }
        }

        public double Diameter
        {
            get
            {
                return this.radius * 2;
            }
            set
            {
                if (this.radius * 2 != value)
                {
                    this.radius = value / 2;
                    this.OnPropertyChanged("Diameter");
                    this.OnPropertyChanged("X");
                    this.OnPropertyChanged("Y");
                }
            }
        }

        #endregion


        #region Constructors

        public CircleViewModel(double x, double y, double radius)
        {
            this.radius = radius;
            this.x = x;
            this.y = y;
        }

        public CircleViewModel(Vector2D point, double radius) 
            : this(point.X, point.Y, radius) 
        {
        }

        #endregion


        #region Methods

        private double GetFalseX()
        {
            return this.x - this.radius;
        }

        private double GetFalseY()
        {
            return this.y - this.radius;
        }

        public override void Scale(double scaleFactor)
        {
            this.radius *= scaleFactor;
            this.x *= scaleFactor;
            this.y *= scaleFactor;

            this.OnPropertyChanged("Diameter");
            this.OnPropertyChanged("X");
            this.OnPropertyChanged("Y");
        }

        public override void FitInSize(double maxWidth, double maxHeight)
        {
            double scaleFactor = maxWidth <= maxHeight
                ? (maxWidth / this.Diameter)
                : (maxHeight / this.Diameter);

            this.Scale(scaleFactor);
        }

        #endregion

        
    }
}
